package com.halixun.d2app.ui;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.halixun.d2app.R;
import com.halixun.d2app.adapter.NavigationAdapter;
import com.halixun.d2app.base.BaseActivity;
import com.halixun.d2app.bean.SettingInfo;
import com.halixun.d2app.ui.fragment.BillWaterFragment;
import com.halixun.d2app.ui.fragment.CommonFragment;
import com.halixun.d2app.ui.fragment.OtherFragment;

import java.util.ArrayList;
import java.util.List;

public class SettingActivity extends BaseActivity implements NavigationAdapter.SettingNavigationListener {

    /**
     * 账单流水tag   financial statement
     */
    public static final String TAG_FINANCIAL_STATEMENT = "financial_statement_tag";

    /**
     * 常规设置tag
     */
    public static final String TAG_CONVENTIN = "convention_tag";

    public static final String TAG_BILL_WATER = "bill_water_tag";

    private RecyclerView navigationRecyclerView;

    private List<SettingInfo> mSettingInfos;

    private NavigationAdapter mNavigationadapter;

    private int clickPosition = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        getSettingInfo();
        hideActionBar();
        initView();
        setAdapter();
        setListeners();
        onSettingNavigationClickListener(0);
    }

    private void setAdapter() {
        mNavigationadapter = new NavigationAdapter(mSettingInfos);
        Log.e("setAdapter",new Gson().toJson(mSettingInfos));
        navigationRecyclerView.setAdapter(mNavigationadapter);
    }

    private void getSettingInfo() {
        if (mSettingInfos == null) {
            mSettingInfos = new ArrayList<>();
        }
        SettingInfo changGui = new SettingInfo();
        changGui.isSelected = true;
        changGui.tag = TAG_FINANCIAL_STATEMENT;
        changGui.title = "财务报表";
        mSettingInfos.add(changGui);
        SettingInfo other = new SettingInfo();
        other.isSelected = false;
        other.tag = TAG_CONVENTIN;
        other.title = "打印设置";
        mSettingInfos.add(other);
        SettingInfo billWater = new SettingInfo();
        billWater.isSelected = false;
        billWater.tag = TAG_BILL_WATER;
        billWater.title = "账单流水";
        mSettingInfos.add(billWater);
    }

    private void setListeners() {
        findViewById(R.id.exit).setOnClickListener(this);
        mNavigationadapter.setOnSettingNavigationListener(this);
    }

    private void initView() {
        navigationRecyclerView = findViewById(R.id.navigationRecyclerView);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.exit:
                finish();
                break;
        }
    }

    @Override
    public void onSettingNavigationClickListener(int position) {
        if (mSettingInfos != null) {
            if (position < mSettingInfos.size()) {
                if (clickPosition != -1) {
                    if (clickPosition != position) {
                        mSettingInfos.get(clickPosition).isSelected = false;
                        mNavigationadapter.notifyItemChanged(clickPosition);
                        mSettingInfos.get(position).isSelected = true;
                        mNavigationadapter.notifyItemChanged(position);
                        setFragmentToRes(mSettingInfos.get(position).tag, mSettingInfos.get(clickPosition).tag);
                    }
                } else {
                    mSettingInfos.get(position).isSelected = true;
                    mNavigationadapter.notifyItemChanged(position);
                    setFragmentToRes(mSettingInfos.get(position).tag, null);
                }
                clickPosition = position;
            }
        }
    }

    private void setFragmentToRes(String tag, String hideTag) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment showFragment = manager.findFragmentByTag(tag);
        Fragment hideFragment = null;
        if (!TextUtils.isEmpty(hideTag)) {
            hideFragment = manager.findFragmentByTag(hideTag);
        }
        if (showFragment == null) {
            transaction.add(R.id.container, getFragment(tag), tag);
        } else {
            transaction.show(showFragment);
        }
        if (hideFragment != null && !tag.equals(hideTag)) {
            transaction.hide(hideFragment);
        }
        transaction.commit();
    }

    private Fragment getFragment(String tag) {
        FragmentManager manager = getSupportFragmentManager();
        Fragment fragment = manager.findFragmentByTag(tag);
        switch (tag) {
            case TAG_FINANCIAL_STATEMENT:
                return fragment == null ? CommonFragment.getInstance(tag) : fragment;
            case TAG_CONVENTIN:
                return fragment == null ? OtherFragment.getInstance(tag) : fragment;
            case TAG_BILL_WATER:
                return fragment == null ? BillWaterFragment.getInstance(tag) : fragment;
            default:
                return null;
        }
    }
}
